package 多线程.创建和使用.实现runnable之多窗口卖票;

/**
 * @Projectname java_based
 * @Filename WindowsTest1
 * @Author an
 * @Data 2022/7/10 16:14
 * @Description TODO
 */

//创建三个窗口卖票，总票数为100，实现Runnable接口
//    存在线程的安全问题
class Windows1 implements Runnable{

    private int ticket = 100;
    @Override
    public void run() {

        while (true){
            if(ticket > 0){


                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }


                System.out.println(Thread.currentThread().getName() + ":" + ticket);
            ticket--;
            }else {
                break;
            }
        }
    }
}


public class WindowsTest1 {
    public static void main(String[] args) {

        Windows1 w = new Windows1();

        Thread t1 = new Thread(w);
        Thread t2 = new Thread(w);
        Thread t3 = new Thread(w);

        t1.setName("窗口1");
        t2.setName("窗口2");
        t3.setName("窗口3");

        t1.start();
        t2.start();
        t3.start();

    }

}